package ldh.im.fxbase.component;

import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import ldh.im.fxbase.util.FileUtil;
import ldh.im.util.UploadFileUtil;

import java.io.File;

public class FileCell extends GridPane {

    private File file;

    public FileCell(File file) {
        this.file = file;

        this.getStyleClass().add("file-cell");
        initUi();
    }

    private void initUi() {
        if(file == null) return;
        Label fileIconLabel = new Label();
        fileIconLabel.getStyleClass().add("file-icon");
        fileIconLabel.setGraphic(FileUtil.getFileIcon(UploadFileUtil.suffix(file.getName())));

        // 文件名称
        Label fileNameLabel = new Label(file.getName());
        fileNameLabel.getStyleClass().add("file-name");

        long size = FileUtil.getFileSize(file);

        // 文件大小
        Label fileSizeLabel = new Label(FileUtil.formatFileSize(size));
        fileSizeLabel.getStyleClass().add("file-size");

        this.getChildren().addAll(fileIconLabel, fileNameLabel, fileSizeLabel);
        GridPane.setConstraints(fileIconLabel, 0, 0, 1, 2);
        GridPane.setConstraints(fileNameLabel, 1, 0, 1, 1);
        GridPane.setConstraints(fileSizeLabel, 1, 1, 1, 1);
    }
}
